﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AutoFacUtils;
using CashFlow.ShopManage.Common;
using CashFlow.ShopManage.Service;

namespace CashFlow.ShopManage.Validator
{
    public class ValidatorTool
    {
        private static IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }
        private static IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }
        private static IShopUserService GetShopUserService() { return AutoFacHelper.Get<IShopUserService>(); }
        private static IShopService GetShopService() { return AutoFacHelper.Get<IShopService>(); }
        private static IOrderService GetOrderService() { return AutoFacHelper.Get<IOrderService>(); }

        public static bool IsUserBelongsMyShop(int userId)
        {
            var adminService = GetAdminService();
            var ui = adminService.GetLoginUserInfo();
            var shopUserService = GetShopUserService();
            bool bSuccess = shopUserService.IsUserBelongsShop(userId, ui.ShopManagerUserId);
            return bSuccess;
        }

        public static bool IsUserIdValiadState(int userId)
        {
            var userService = GetUserService();
            var entity = userService.QuerySingleById(userId);
            if (entity != null && entity.State == 1)
            {
                return true;
            }
            return false;
        }

        public static bool IsValidInvitationCodeInShop(int userId
            , string invitationCode)
        {
            var adminService = GetAdminService();
            var ui = adminService.GetLoginUserInfo();
            var userService = GetUserService();
            var ls = userService.QueryShopAllUsersByShopManagerUserId(
                        ui.ShopManagerUserId
                        , null
                     );
            int count = ls.Where(x => x.Id == userId
                            && x.InvitationCode == invitationCode)
                          .Count();
            return count > 0;
        }

        public static bool IsValidInvitationCodeInShop2(int userId
            , string invitationCode)
        {
            var adminService = GetAdminService();
            var ui = adminService.GetLoginUserInfo();
            var userService = GetUserService();
            var ls = userService.QueryShopAllUsersByShopManagerUserId(
                        ui.ShopManagerUserId
                        , null
                     );
            int count = ls.Where(x => x.Id != userId
                            && x.InvitationCode == invitationCode)
                          .Count();
            return count > 0;
        }

        public static bool IsValidShopOrderId(int orderId)
        {
            var adminService = GetAdminService();
            var ui = adminService.GetLoginUserInfo();
            var orderService = GetOrderService();
            string date = MyCommonTool.GetOrderDate();
            return orderService.IsExistOrderIdInShop(
                        ui.ShopManagerUserId
                        , orderId
                        , date
                   );
        }
    }
}